package com.liu.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.liu.sunnyday.dao.MemberDao;
import com.liu.sunnyday.dao.OrderDao;
import com.liu.sunnyday.service.ReportService;
import com.liu.sunnyday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * chance！Time
 *
 * @author liuqian
 * @date 2022-07-11 17:48
 * *
 */
@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 运营统计的全部查询
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
        /**      reportDate（当前时间）--String
         *      todayNewMember（今日新增会员数） -> number
         *      totalMember（总会员数） -> number
         *      thisWeekNewMember（本周新增会员数） -> number
         *      thisMonthNewMember（本月新增会员数） -> number
         *      todayOrderNumber（今日预约数） -> number
         *      todayVisitsNumber（今日到诊数） -> number
         *      thisWeekOrderNumber（本周预约数） -> number
         *      thisWeekVisitsNumber（本周到诊数） -> number
         *      thisMonthOrderNumber（本月预约数） -> number
         *      thisMonthVisitsNumber（本月到诊数） -> number
         *      hotSetmeal（热门套餐（取前4）） -> List<Map<String,Object>>
         */
        //时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //1,当天时间
        String today = sdf.format(new Date());
        //2,根据当前时间拿到周一的时间
        String monday = sdf.format(DateUtils.getThisWeekMonday());
        //3,根据当前时间拿到当月一号的时间
        String firstDayOfThisMonth = sdf.format(DateUtils.getFirstDay4ThisMonth());
        //4,本周的周天
        String sunday = sdf.format(DateUtils.getSundayOfThisWeek());
        //5,本月的最后一天
        String lastDayOfThisMonth=sdf.format(DateUtils.getSundayOfThisWeek());



        //今日新增--参数：当天的时间
        int todayNewMember = memberDao.findMemberCountByDate(today);
        //总会员数
        int totalMember = memberDao.findMemberTotalCount();
        //本周新增会员数--参数：周一的时间
        /**
         * “本周，本月”可以有一种新思路
         * 就是根据现在时间拿到本周一，然后判断周一之后的所有数据
         * （不可能出现本周之后注册的用户）如今天是周天不可能有下周一注册的用户，
         */
        int thisWeekNewMember = memberDao.findMemberCountAfterDate(monday);
        //本月新增会员数--参数：本月第一天
        int thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDayOfThisMonth);

        //今日预约数--参数：当天时间
        int todayOrderNumber = orderDao.findOrderCountByDate(today);
        //今日到诊数--参数：当天时间
        int todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周预约数--参数：周一时间，周天时间
        /**
         * 本周预约数：时间在周一到周天之间
         *不像本周新增会员一样，可以判断今天之后一定没有新增会员，
         * 预约数可以在今天之后所以要本周最后一天的时间
         */
        int thisWeekOrderNumber = orderDao.findOrderCountBetweenDate(monday, sunday);
        //本周到诊数
        int thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(monday);
        //本月预约数-- 参数：本月1号和本月最后一天
        int thisMonthOrderNumber=orderDao.findOrderCountBetweenDate(firstDayOfThisMonth,lastDayOfThisMonth);
        //本月到诊数
        int thisMonthVisitsNumber=orderDao.findVisitsCountAfterDate(firstDayOfThisMonth);
        // ============== 热门套餐 ==================
        //hotSetmeal
        List<Map<String, Object>> hotSetmeal = orderDao.findHotSetmeal();

        //将拿到的数据封装返回
        Map<String,Object> businessReport=new HashMap<String,Object>();
        businessReport.put("reportDate" ,today);
        businessReport.put("todayNewMember" ,todayNewMember);
        businessReport.put("totalMember" ,totalMember);
        businessReport.put("thisWeekNewMember" ,thisWeekNewMember);
        businessReport.put("thisMonthNewMember" ,thisMonthNewMember);
        businessReport.put("todayOrderNumber" ,todayOrderNumber);
        businessReport.put("todayVisitsNumber" ,todayVisitsNumber);
        businessReport.put("thisWeekOrderNumber" ,thisWeekOrderNumber);
        businessReport.put("thisWeekVisitsNumber" ,thisWeekVisitsNumber);
        businessReport.put("thisMonthOrderNumber" ,thisMonthOrderNumber);
        businessReport.put("thisMonthVisitsNumber" ,thisMonthVisitsNumber);
        businessReport.put("hotSetmeal" ,hotSetmeal);
        return businessReport;
    }
}
